

#ifndef _DLOAD_H__
#define _DLOAD_H__

#include "global.h"
//#include "fdl.h"
#include "cutils/properties.h"

#define PACHET_HDR_LEN     sizeof(Packet_Header)

#define PACKET_MAX_NUM   4
#define MAX_PKT_SIZE            (0x2000)  
#define PNULL		0
#define LOCAL static
#define FLAG_BYTE               0x7E
#define HDLC_FLAG               0x7E
#define HDLC_ESCAPE             0x7D
#define HDLC_ESCAPE_MASK        0x20


typedef signed short cmd_pkt_type;
#define PACKET_HEADER_SIZE   4   // (type + size)

typedef enum _CMD_TYPE
{
    BSL_PKT_TYPE_MIN = 0,                       /* the bottom of the DL packet type range */
		BSL_CMD_TYPE_MIN = BSL_PKT_TYPE_MIN,        /* 0x0 */
		
		/* Link Control */
		BSL_CMD_CONNECT = BSL_CMD_TYPE_MIN,         /* 0x0 */
		/* Data Download */
		/* the start flag of the data downloading */
		BSL_CMD_START_DATA,                         /* 0x1 */
		/* the midst flag of the data downloading */
		BSL_CMD_MIDST_DATA,                         /* 0x2 */
		/* the end flag of the data downloading */
		BSL_CMD_END_DATA,                           /* 0x3 */
		/* Execute from a certain address */
		BSL_CMD_EXEC_DATA,                          /* 0x4 */
		BSL_CMD_NORMAL_RESET,                       /* 0x5 */
		BSL_CMD_READ_FLASH,                         /* 0x6 */
		BSL_CMD_READ_CHIP_TYPE,                     /* 0x7 */
		BSL_CMD_LOOKUP_NVITEM,                      /* 0x8 */
		BSL_SET_BAUDRATE,                           /* 0x9 */
		BSL_ERASE_FLASH,                            /* 0xA */
		BSL_REPARTITION,                            /* 0xB */
		
		// added by zhl:
		BSL_CMD_READ_SECTOR_SIZE = 0xf,
		BSL_CMD_READ_MCP_TYPE,
		
		BSL_CMD_TYPE_MAX,
		BSL_CMD_CHECK_BAUD	= FLAG_BYTE,
		/* Start of the Command can be transmited by phone*/
		BSL_REP_TYPE_MIN = 0x80,
		
		/* The operation acknowledge */
		BSL_REP_ACK = BSL_REP_TYPE_MIN,         /* 0x80 */
		BSL_REP_VER,                            /* 0x81 */
		
		/* the operation not acknowledge */
		/* system  */
		BSL_REP_INVALID_CMD,                    /* 0x82 */
		BSL_REP_UNKNOW_CMD,                     /* 0x83 */
		BSL_REP_OPERATION_FAILED,               /* 0x84 */
		
		/* Link Control*/
		BSL_REP_NOT_SUPPORT_BAUDRATE,           /* 0x85 */
		
		/* Data Download */
		BSL_REP_DOWN_NOT_START,                 /* 0x86 */
		BSL_REP_DOWN_MULTI_START,               /* 0x87 */
		BSL_REP_DOWN_EARLY_END,                 /* 0x88 */
		BSL_REP_DOWN_DEST_ERROR,                /* 0x89 */
		BSL_REP_DOWN_SIZE_ERROR,                /* 0x8A */
		BSL_REP_VERIFY_ERROR,                   /* 0x8B */
		BSL_REP_NOT_VERIFY,                     /* 0x8C */
		
		/* Phone Internal Error */
		BSL_PHONE_NOT_ENOUGH_MEMORY,            /* 0x8D */
		BSL_PHONE_WAIT_INPUT_TIMEOUT,           /* 0x8E */
		
		/* Phone Internal return value */
		BSL_PHONE_SUCCEED,                      /* 0x8F */
		BSL_PHONE_VALID_BAUDRATE,               /* 0x90 */
		BSL_PHONE_REPEAT_CONTINUE,              /* 0x91 */
		BSL_PHONE_REPEAT_BREAK,                 /* 0x92 */
		
		BSL_REP_READ_FLASH,                     /* 0x93 */
		BSL_REP_READ_CHIP_TYPE,                 /* 0x94 */
		BSL_REP_LOOKUP_NVITEM,                  /* 0x95 */
		
		BSL_INCOMPATIBLE_PARTITION,             /* 0x96 */
		BSL_UNKNOWN_DEVICE,                     /* 0x97 */
		BSL_INVALID_DEVICE_SIZE,                /* 0x98 */
		
		BSL_ILLEGAL_SDRAM,                      /* 0x99 */
		BSL_WRONG_SDRAM_PARAMETER,              /* 0x9a */
		BSL_REP_READ_SECTOR_SIZE =0x9c,	// added by zhl
		BSL_REP_READ_MCP_TYPE,
		BSL_EEROR_CHECKSUM = 0xA0,
		BSL_CHECKSUM_DIFF,
		BSL_WRITE_ERROR,
		BSL_UDISK_IMAGE_SIZE_OVERFLOW,          /* 0xa3 */
		BSL_CHIPID_NOT_MATCH		= 0xA3,
		BSL_FLASH_CFG_ERROR= 0xa4,              /* 0xa4 */
		BSL_REP_DOWN_STL_SIZE_ERROR,
		BSL_REP_LOG = 0xFF,                             /* 0xff */
		BSL_ERROR_CHECKSUM			= 0xA0,

    BSL_PKT_TYPE_MAX = 0x100,

    BSL_UART_SEND_ERROR,
    BSL_REP_DECODE_ERROR,       // Decode or verify received buffer error
    BSL_REP_INCOMPLETE_DATA,     // Received buffer is not in the format we want
    BSL_REP_READ_ERROR,
    BSL_REP_TOO_MUCH_DATA,
    BSL_USER_CANCEL,
	BSL_REP_SIZE_ZERO,

		//added by zhl:
		BSL_CMD_FDL_VER = 0x500,
		BSL_REP_VER_ERROR,
		BSL_CMD_MCU_TYPE,
		BSL_CMD_IMAGE_VER,
		BSL_CMD_UITEST_FLAG,
		
		//BSL_PKT_TYPE_MAX
} CMD_TYPE;

typedef struct Packet_Header_Tag
{
    short   PacketType;
    short   DataSize;    
}Packet_Header,*P_Packet_Header;

typedef struct _BM_HEADER
{
	unsigned short type;
	unsigned short len;
}BM_HEADER;

typedef struct _BM_PACKAGE
{
	BM_HEADER header;
	void* data;
}BM_PACKAGE;

typedef struct _PRT_WRITE_T
{
	int action;				// Write action	
	int nCond;				// Condition id returned if a Read will be called after Write
	void* lpProtocolData;	// The data to be write
}PRT_WRITE_T;

BOOL CheckBaud();
BOOL CheckBaudRom() ;
BOOL Connect();
BOOL ConnectRom();
BOOL Download_m7281(UINT nAddress, long pFileInfo);
BOOL Download_m1190(UINT nAddress, long pFileInfo);
BOOL DownloadFDL_m7281(UINT nAddress,long pFileInfo);
BOOL DownloadFDL_m1190(UINT nAddress,long pFileInfo);
BOOL DownloadFDL2(UINT nAddress,long pFileInfo);
BOOL DownloadFile(UINT nAddress,long pFileInfo);
BOOL DownloadNV_m7281(UINT nAddress,long pFileInfo);
BOOL DownloadNV_m1190(UINT nAddress,long pFileInfo);
BOOL DownloadNV_m0282(UINT nAddress,long pFileInfo);
BOOL FormatFlash(UINT nAddress,long pFileInfo);
BYTE* GetFlashName();
//BOOL SendData( BM_PACKAGE* lpPkg,DWORD dwTimeout );
//BOOL SendCommandData( pkt_type_s nCmdType , DWORD dwTimeout);
BOOL StopOpr();

BOOL DownloadKernel(UINT nAddress, long pFileInfo);
BOOL EraseFlash( DWORD dwBase,DWORD dwLength);

extern struct DownloadCallback mDownloadCallback;

DWORD  m_dwLastErrorCode;
CMD_TYPE  m_iLastSentPktType;
BOOL m_bOperationSuccess;


#endif 
